package contacts.controller;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;

import com.eltima.components.ui.DatePicker;

import contacts.po.Room;
import contacts.po.Student;
import contacts.service.ContactsService;
import contacts.view.ContactsDialog;
import contacts.view.ContactsFrame;
import contacts.view.LogIn;

public class ContactsControllerListener implements ActionListener{
	private ContactsFrame contactsFrame;
	private ContactsDialog contactsDialog;
	private ContactsService contactsService;
	
	private LogIn logIn;
	
	public ContactsControllerListener() {
		contactsService=new ContactsService();
	}
	
	
	
	public void actionPerformed(ActionEvent e) {
		if("取消".equals(e.getActionCommand())) {	
			JOptionPane.showMessageDialog(logIn,"取消登录");
		}
		else if("确认".equals(e.getActionCommand())) {
//			JOptionPane.showMessageDialog(logIn,"登录成功");
//			logIn.dispose();
//			contactsFrame=new ContactsFrame();
			String passWordInput = logIn.getjPasswordField().getText().trim();
			String nameInput = logIn.getjTextField().getText().trim();
			if(passWordInput!=null&&nameInput!=null&&!"".equals(passWordInput)&&!"".equals(nameInput)) {
				JOptionPane.showMessageDialog(logIn,"登录成功");
				logIn.dispose();
				contactsFrame=new ContactsFrame();
			}
			else {
				JOptionPane.showMessageDialog(logIn,"请输入用户名和密码");
			}
		}
		
		else if("addStudentDialog".equals(e.getActionCommand())) {
		contactsDialog = new ContactsDialog(contactsFrame,"添加学生");
		}
		
		else if("addRoomDialog".equals(e.getActionCommand())) {
			contactsDialog = new ContactsDialog("添加宿舍",contactsFrame);
		}
		else if("addPost".equals(e.getActionCommand())) {
			Student newStudent=this.getStudentInstance();
			if(newStudent!=null) {
				contactsService.addStudent(newStudent);
				JOptionPane.showMessageDialog(contactsFrame,"添加"+newStudent.getId()+"号学生成功！");
				contactsDialog.dispose();
				contactsFrame.refreshStudent(contactsService.findAllStudent());
				
			}
			else {
				JOptionPane.showMessageDialog(contactsFrame,"添加人数失败");
			}
			
		}		
		else if("addPost2".equals(e.getActionCommand())) {
			Room newRoom=this.getRoomInstance();
			if(newRoom!=null) {
				contactsService.addRoom(newRoom);
				JOptionPane.showMessageDialog(contactsFrame,"添加"+newRoom.getRoomNo()+"号宿舍成功！");
				contactsDialog.dispose();
				contactsFrame.refreshRoom(contactsService.findAllRoom());
				
			}
			else {
				JOptionPane.showMessageDialog(contactsFrame,"添加宿舍失败");
			}			
		}		
		else if("searchStudentDialog".equals(e.getActionCommand())) {
			List<Student> studentList=new ArrayList<Student>();
			for(Student student:contactsService.findAllStudent()) {
				studentList.add(student);
			}
			String str=JOptionPane.showInputDialog(contactsFrame,"输入学生编号");
			if(str!=null && !"".equals(str)) {
				if(str.matches("^[0-9]*$")) {
					int id=Integer.parseInt(str);
					Student student=new Student();
					student.setId(id);
					List<Student> result=contactsService.searchById(studentList, student);
					contactsFrame.showSearchResult(result);
				}
				else {
					JOptionPane.showMessageDialog(contactsFrame," 输入的不是数字","输入错误",JOptionPane.ERROR_MESSAGE);
				}
			}
			
		}		
		else if("searchRoomDialog".equals(e.getActionCommand())) {
			List<Room> roomList=new ArrayList<Room>();
			for(Room room:contactsService.findAllRoom()) {
				roomList.add(room);
			}
			String str=JOptionPane.showInputDialog(contactsFrame,"输入宿舍学生编号");
			if(str!=null && !"".equals(str)) {
				if(str.matches("^[0-9]*$")) {
					int sRoomNo=Integer.parseInt(str);
					Room room=new Room();
					room.setRoomNo(sRoomNo);
					List<Room> result=contactsService.searchByroomNo(roomList, room);
					contactsFrame.showSearchResult2(result);
				}
				else {
					JOptionPane.showMessageDialog(contactsFrame," 输入的不是数字","输入错误",JOptionPane.ERROR_MESSAGE);
				}
			}
			
		}		
		else if("updateStudentDialog".equals(e.getActionCommand())) {
			int selRow=contactsFrame.getSelRow();
			if(selRow<0) {
				JOptionPane.showMessageDialog(contactsFrame, "请选择一行数据!");
				
			}
			else {
				Student student=contactsService.findAllStudent().get(contactsFrame.getSelRow());
				contactsDialog=new ContactsDialog(contactsFrame,"修改学生信息",student);//
				
			}
		}		
		else if("updateRoomDialog".equals(e.getActionCommand())) {
			int selRow=contactsFrame.getSelRow();
			if(selRow<0) {
				JOptionPane.showMessageDialog(contactsFrame, "请选择一行数据!");
				
			}
			else {
				Room room=contactsService.findAllRoom().get(contactsFrame.getSelRow());
				contactsDialog=new ContactsDialog(contactsFrame,"修改宿舍",room);//
				
			}
		}		
		else if("updatePost".equals(e.getActionCommand())) {
			Student updatedStudent=this.getStudentInstance();
			if(updatedStudent!=null) {
				contactsService.updateStudent(updatedStudent);
				JOptionPane.showMessageDialog(contactsFrame,updatedStudent.getId()+"号学生修改成功！");
				contactsDialog.dispose();
				contactsFrame.refreshStudent(contactsService.findAllStudent());				
			}
			else {
				JOptionPane.showMessageDialog(contactsFrame,"更新人数失败");
			}			
		}
		
		else if("updatePost2".equals(e.getActionCommand())) {
			Room updatedRoom=this.getRoomInstance();
			if(updatedRoom!=null) {
				contactsService.updateRoom(updatedRoom);
				JOptionPane.showMessageDialog(contactsFrame,updatedRoom.getRoomNo()+"号宿舍修改成功！");
				contactsDialog.dispose();
				contactsFrame.refreshRoom(contactsService.findAllRoom());				
			}
			else {
				JOptionPane.showMessageDialog(contactsFrame,"更新宿舍失败");
			}			
		}		
		else if("deleteStudentDialog".equals(e.getActionCommand())) {
			int selRow=contactsFrame.getSelRow();
			if(selRow<0) {
				JOptionPane.showMessageDialog(contactsFrame, "请选择一行数剧！");
			}else {
				Student student=contactsService.findAllStudent().get(contactsFrame.getSelRow());
				contactsService.deleteStudent(student.getId());
				JOptionPane.showMessageDialog(contactsFrame, "删除"+student.getId()+"号同学成功！");
				contactsFrame.refreshStudent(contactsService.findAllStudent());
			}
		}		
		else if("deleteRoomDialog".equals(e.getActionCommand())) {
			int selRow=contactsFrame.getSelRow();
			if(selRow<0) {
				JOptionPane.showMessageDialog(contactsFrame, "请选择一行数剧！");
			}else {
				Room room=contactsService.findAllRoom().get(contactsFrame.getSelRow());
				contactsService.deleteRoom(room.getRoomNo());
				JOptionPane.showMessageDialog(contactsFrame, "删除"+room.getRoomNo()+"号宿舍成功！");
				contactsFrame.refreshRoom(contactsService.findAllRoom());
			}
		}				
		else if("refreshStudentFrame".equals(e.getActionCommand())) {
			contactsFrame.refreshStudent(contactsService.findAllStudent());
		}
		
		else if("refreshRoomFrame".equals(e.getActionCommand())) {
			contactsFrame.refreshRoom(contactsService.findAllRoom());
		}
		
	}		
	public Student getStudentInstance()  {
		int id=0;
		String sNumber=null;
		String sName=null;
		java.sql.Date birthday=null;
		String tel=null;
		String email=null;
		String houseAddress=null;
		int sRoomNo=0;
		String sRoomName=null;
		Student student=null;		
		String idInput=contactsDialog.getIdTextField().getText().trim();
		if(idInput!=null&&!"".equals(idInput)) {
			if(idInput.matches("^[0-9]*$")) {
				id=Integer.parseInt(idInput);
			}
			else {
				JOptionPane.showMessageDialog(contactsFrame, "ID必须是整数类型","输入错误",JOptionPane.ERROR_MESSAGE);
			}
		}
		else {
			JOptionPane.showMessageDialog(contactsFrame, "ID不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}		
		String sNumberInput = contactsDialog.getsNumberTextField().getText().trim();
		if(sNumberInput != null && !"".equals(sNumberInput)) {
			sNumber = sNumberInput;
		}else {
			JOptionPane.showMessageDialog(contactsFrame, "学号不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}		
		String sNameInput = contactsDialog.getsNameTextField().getText().trim();
		if(sNameInput != null && !"".equals(sNameInput)) {
			sName = sNameInput;
		}else {
			JOptionPane.showMessageDialog(contactsFrame, "姓名不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
	
		String birthdayInput = contactsDialog.getBirthdayTextField().getText().trim();
		if(birthdayInput != null && !"".equals(birthdayInput)) {
			birthday =java.sql.Date.valueOf(birthdayInput);
		}else {
			JOptionPane.showMessageDialog(contactsFrame, "生日不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		
		String telInput = contactsDialog.getTelTextField().getText().trim();		
		if(telInput != null && !"".equals(telInput)) {
			tel= telInput;
		}else {
			JOptionPane.showMessageDialog(contactsFrame, "电话号码不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		
		String emailInput = contactsDialog.getEmailTextField().getText().trim();
		if(emailInput != null && !"".equals(emailInput)) {
			email = emailInput;
		}else {
			JOptionPane.showMessageDialog(contactsFrame, "邮件不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		
		String houseAddressInput = contactsDialog.getHouseAddressTextField().getText().trim();
		if(houseAddressInput != null && !"".equals(houseAddressInput)) {
			houseAddress = houseAddressInput;
		}else {
			JOptionPane.showMessageDialog(contactsFrame, "地址不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		
		String sRoomNoInput  = contactsDialog.getsRoomNoTextField().getText().trim();
		if(sRoomNoInput != null && !"".equals(sRoomNoInput)) {
			if(sRoomNoInput.matches("^[0-9]*$")){	
				sRoomNo = Integer.parseInt(sRoomNoInput);
				
			}
			else {
				JOptionPane.showMessageDialog(contactsFrame, "宿舍号必须是整数类型","输入错误",JOptionPane.ERROR_MESSAGE);
			}
		}
		else {
			JOptionPane.showMessageDialog(contactsFrame, "宿舍号不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		sRoomName=" ";
		Room room=new Room(sRoomNo,sRoomName);
		if(id>0 && sNumber!=null && sName!=null &&  birthday!=null && tel!=null &&
				email!=null && houseAddress!=null && sRoomNo>0&& sRoomName!=null) 
		{
			student=new Student(id, sNumber, sName, birthday, tel,  email, houseAddress,room);
		}
		return student;
	}
	
	public Room getRoomInstance()  {
		int sRoomNo=0;
		String sRoomName=null;
		Room room=null;
		
		String sRoomNoInput=contactsDialog.getsRoomNoTextField2().getText().trim();
		if(sRoomNoInput!=null&&!"".equals(sRoomNoInput)) {
			if(sRoomNoInput.matches("^[0-9]*$")) {
				sRoomNo=Integer.parseInt(sRoomNoInput);
			}
			else {
				JOptionPane.showMessageDialog(contactsFrame, "sRoomNo必须是整数类型","输入错误",JOptionPane.ERROR_MESSAGE);
			}
		}
		else {
			JOptionPane.showMessageDialog(contactsFrame, "sRoomNo不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		String sRoomNameInput = contactsDialog.getsRoomNameTextField2().getText().trim();
		if(sRoomNameInput != null && !"".equals(sRoomNameInput)) {
			sRoomName = sRoomNameInput;
		}else {
			JOptionPane.showMessageDialog(contactsFrame, "宿舍名地址为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		
		if(sRoomNo>0  && sRoomName!=null) 
		{
			room=new Room(sRoomNo,sRoomName);
		}
		return room;
	}
	
	public void setContactsFrame(ContactsFrame contactsFrame) {
		this.contactsFrame = contactsFrame;
	}
	public void setDenglu(LogIn logIn) {
		this.logIn = logIn;
	}
	public ContactsService getContactsService() {
		return contactsService;
	}
	public void setContactsAddDialog(ContactsDialog contactsAddDialog) {
		this.contactsDialog = contactsAddDialog;
	}
	
}
